local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local theme = require("theme")
local dpi	= require("beautiful.xresources").apply_dpi










local handler = awful.popup{
	widget = wibox.widget{
			{
				--forced_height = theme.handler_icon_size,
				forced_width = theme.handler_icon_size,
				opacity = theme.handler_icon_opacity,
				image  = theme.handler_icon,
				resize = true,
				valign = "center",
				halign = "center",
				widget = wibox.widget.imagebox,
			},
			forced_height = theme.handler_height,
			forced_width = theme.handler_width,
			valign = "center",
			halign = "center",
			widget	= wibox.container.place,
	},
	minimum_width   = theme.handler_width,
	minimum_height 	= theme.handler_height,
	border_color	= theme.handler_border_color,
    border_width	= theme.handler_border_width,
	visible			= false,
	bg				= theme.handler_bg,
	fg				= theme.popup_fg,
	ontop			= true,
	shape			= function(cr, width, height)
		gears.shape.rounded_rect(cr, width, height, theme.handler_border_radius)
	end,
}



handler:buttons(gears.table.join(
	awful.button({ }, 1, function ()
        handler.client:emit_signal("request::activate", "mouse_click", {raise = true})
		awful.mouse.client.move(handler.client)
	end),
	awful.button({ }, 2, function ()
		handler.visible = false
		handler.client:kill()
	end),
	awful.button({ }, 3, function ()
		handler.client.minimized = true
		handler.visible = false
	end)
))




client.connect_signal("mouse::leave", function (c)
	if client.focus == c and not handler.visible then
		local coords = mouse.coords()
		local geo = c:geometry()
		if coords.y < geo.y + theme.handler_trigger_range then
			handler.x = geo.x + math.floor(geo.width/2) - math.floor(theme.handler_width/2)
			handler.y = geo.y - math.floor(theme.handler_height/2)
			handler.client = c
			handler.timer:again()
			handler.visible = true
		end
	end
end)




client.connect_signal("mouse::enter", function (c)
	if client.focus == c and not handler.visible then
		local geo = c:geometry()
		handler.x = geo.x + math.floor(geo.width/2) - math.floor(theme.handler_width/2)
		handler.y = geo.y - math.floor(theme.handler_height/2)
		handler.client = c
		handler.timer:again()
		handler.visible = true
	end
end)


client.connect_signal("property::geometry", function(c)
	if handler.visible and client.focus == c then
		handler.x = c.x + math.floor(c.width/2) - math.floor(theme.handler_width/2)
		handler.y = c.y - math.floor(theme.handler_height/2)
		handler.timer:again()
	end
end)


--a = 0
--tag.connect_signal("property::selected", function(t)
--	local s = awful.screen.focused()
--	if t == s.selected_tag then
--		a = a + 1
--		print("selected false: " .. a)
--		local clients = t:clients()		
--		if #clients == 0 then
--			handler.visible = false
--		else
--			for _, c in ipairs(clients) do
--				if not c.minimized then
--					return
--				else
--					handler.visible = false
--				end
--			end
--		end
--	end
--end)



client.connect_signal("focus", function(c) 
	handler.x = c.x + math.floor(c.width/2) - math.floor(theme.handler_width/2)
	handler.y = c.y - math.floor(theme.handler_height/2)
	handler.client = c
	handler.timer:again()
	handler.visible = true
	local s = awful.screen.focused()
	if #s.clients == 1 then
		handler.bg = theme.handler_normal
	else
		handler.bg = theme.handler_bg
	end
end)

a=0
client.connect_signal("unfocus", function(c) 
		handler.unfocus_timer:again()
end)

client.connect_signal("untagged", function(c) 
	local s = awful.screen.focused()
	if #s.clients >= 1 and client.focus ~= nil then
		handler.bg = theme.handler_normal
		handler.visible = true
	else
		handler.visible = false
	end
end)




handler.timer = gears.timer {
	timeout   = theme.handler_timeout,
	call_now  = false,
	autostart = false,
	single_shot = false,
	callback  = function()
		--print("timer")
		local coords = mouse.coords()
		if coords.x < handler.x or coords.x > handler.x + theme.handler_width or coords.y < handler.y or coords.y > handler.y + theme.handler_height then
			handler.visible = false
			--print("timer false")
			handler.timer:stop()
		end
	end
}

handler.unfocus_timer = gears.timer {
	timeout   = 0.05,
	call_now  = false,
	autostart = false,
	single_shot = true,
	callback  = function()
		if client.focus == nil then
			handler.visible = false
		end
	end
}




function handler:setup()
	return handler
end




return handler
